package com.tgjorgoski.sidekicknotes.rtfview.support;

import java.awt.event.ActionEvent;
import java.util.*;

import javax.swing.Action;
import javax.swing.text.*;
import javax.swing.text.html.HTMLDocument;

import com.tgjorgoski.sidekicknotes.htmlview.HTMLDocChanged;
import com.tgjorgoski.sidekicknotes.htmlview.HTMLEditor;

public class IndentSupport {
	public static final String INDENT_KEY="indent-action";
	public static final String UNINDENT_KEY="unindent-action";
	
	public Action indentAction = new IndentAction();
	public Action unindentAction = new UnindentAction();
	private static IndentSupport instance;
	
	public static IndentSupport getInstance() {
		if (instance==null) {
			instance=new IndentSupport();
		}
		return instance;
	}
	
	private IndentSupport() {
		indentAction = new IndentAction();
		unindentAction = new UnindentAction();
	}
	
	public void registerComponent(JTextComponent comp) {
		comp.getActionMap().put(INDENT_KEY, indentAction);
		comp.getActionMap().put(UNINDENT_KEY, unindentAction);
	}
	
	public class IndentAction extends TextAction {
		public IndentAction() {
			super(INDENT_KEY);
		}
		public void actionPerformed(ActionEvent e) {
			JTextComponent comp = getTextComponent(e);
			if (comp instanceof HTMLEditor) {
				HTMLEditor htmlEd = (HTMLEditor)comp;
				int sPosition=htmlEd.getSelectionStart();
				int ePosition=htmlEd.getSelectionEnd();
				HTMLDocChanged htmlDoc = (HTMLDocChanged)htmlEd.getDocument();
				if (sPosition==ePosition) {
					try {
						htmlDoc.insertString(sPosition, "\u00A0\u00A0\u00A0\u00A0", htmlEd.getInputAttributes());
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} else {
					List<Element> paragraphs = htmlDoc.getParagraphs(sPosition, ePosition);
					for (Element para : paragraphs) {
						int sOffset = para.getStartOffset();
						try {
							//htmlDoc.insertAfterStart(para, "&#160;&#160;&#160;&#160;");
							htmlDoc.insertString(sOffset, "\u00A0\u00A0\u00A0\u00A0", htmlEd.getInputAttributes());
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
			}
		}		
	}
	
	public class UnindentAction extends TextAction {
		public UnindentAction() {
			super(UNINDENT_KEY);
		}
		public void actionPerformed(ActionEvent e) {
			JTextComponent comp = getTextComponent(e);
			if (comp instanceof HTMLEditor) {
				HTMLEditor rtf = (HTMLEditor)comp;
				AttributeSet parAttributes= rtf.getParagraphAttributes();
				int sPosition=rtf.getSelectionStart();
				int ePosition=rtf.getSelectionEnd();
				HTMLDocChanged sDoc = (HTMLDocChanged)rtf.getDocument();
				List<Element> paragraphs = sDoc.getParagraphs(sPosition, ePosition);
				for (Element para : paragraphs) {
					int sOffset = para.getStartOffset();
					boolean removed=false;
					try {
						String startCh4 = sDoc.getText(sOffset, 4);
						if (startCh4.equals("\u00A0\u00A0\u00A0\u00A0")) {
							sDoc.remove(sOffset, 4);
							removed=true;
						}
					} catch (BadLocationException e1) {
						// TODO Auto-generated catch block
						//e1.printStackTrace();
					}
					if (!removed) {
						try {
							String startCh1 = sDoc.getText(sOffset, 1);
							if (startCh1.equals(" ") || startCh1.equals("\u00A0")) {
								sDoc.remove(sOffset, 1);
								removed=true;
							}
						} catch (BadLocationException e1) {
							// TODO Auto-generated catch block
							//e1.printStackTrace();
						}
					}
				}
			}
		}		
	}
}
